﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using STLGROUP.Layers.Presentations.FormBase;
using STLGROUP.Layers.DataAccessLayers;
using STLGROUP.Layers.Common;
using STLGROUP.Layers.BLL;

namespace QuanLyBanHang
{
    /// <summary>
    /// MF003 Quan Ly Thong Tin Nhan Vien
    /// </summary>
    /// <author>Van Long</author>
    /// <date>07/04/2012</date>
    public partial class MF003 : frmManager
    {

        #region Member variables

        DT_NhanVienBLL _business;
        DT_NhanVien _current;
        ToolStripButton _btnCapNhatTrangThai;

        #endregion

        #region Constructors

        public MF003()
        {
            InitializeComponent();

            InitializeForm();
        }

        #endregion

        #region Protected methods

        protected override void LoadData()
        {
            this._bindingSource.DataSource = Cache.Context.DT_NhanVien;
            this.dgvNhanVien.DataSource = _bindingSource;
            base.LoadData();
        }

        protected override void ImportFormExcel()
        {
            ImportDataFromExcel<DT_NhanVien>(dgvNhanVien);
        }

        protected override void ExportToExcel()
        {
            ExportDataToExcel(dgvNhanVien);
        }

        protected override void AddDataBinding()
        {
            txtMaNhanVien.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "MaNhanVien", true);
            txtHoVaTenDem.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "HoVaTenDem", true);
            txtTenNhanVien.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "TenNhanVien", true);
            chbxGioiTinh.DataBindings.Add(Const.BIND_CHECKED, _bindingSource, "GioiTinh", true);
            dtiNgaySinh.DataBindings.Add(Const.BIND_VALUE, _bindingSource, "NgaySinh", true);
            txtSoCMND.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "SoCMND", true);
            chbxDaKetHon.DataBindings.Add(Const.BIND_CHECKED, _bindingSource, "DaKetHon", true);
            txtDiaChi.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "DiaChi", true);
            txtEmail.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "Email", true);
            txtSoDienThoai.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "SoDienThoai", true);
            txtFax.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "Fax", true);
            txtGhiChu.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "GhiChu", true);

            cboDanToc.DataSource = Cache.Context.DM_DanToc;
            cboDanToc.ValueMember = "IDDanToc";
            cboDanToc.DisplayMember = "TenDanToc";
            cboDanToc.DataBindings.Add(Const.BIND_SELECTED_VALUE, _bindingSource, "IDDanToc", true);

            cboTonGiao.DataSource = Cache.Context.DM_TonGiao;
            cboTonGiao.ValueMember = "IDTonGiao";
            cboTonGiao.DisplayMember = "TenTonGiao";
            cboTonGiao.DataBindings.Add(Const.BIND_SELECTED_VALUE, _bindingSource, "IDTonGiao", true);

            cboPhongBan.DataSource = Cache.Context.DM_PhongBan;
            cboPhongBan.ValueMember = "IDPhongBan";
            cboPhongBan.DisplayMember = "TenPhongBan";
            cboPhongBan.DataBindings.Add(Const.BIND_SELECTED_VALUE, _bindingSource, "IDPhongBan", true);

            //cboChucVu.DataSource = Cache.Context.DM_ChucVu;
            //cboChucVu.ValueMember = "IDChucVu";
            //cboChucVu.DisplayMember = "TenChucVu";
            //cboChucVu.DataBindings.Add(Const.BIND_SELECTED_VALUE, _bindingSource, "IDChucVu", true);

            cboChucVu.DataSource = Cache.Context.ACL_NhomNguoiDung;
            cboChucVu.ValueMember = "IDNhomNguoiDung";
            cboChucVu.DisplayMember = "TenNhom";
            cboChucVu.DataBindings.Add(Const.BIND_SELECTED_VALUE, _bindingSource, "IDChucVu", true);

            cboLoaiNhanVien.DataSource = Cache.Context.DM_LoaiNhanVien;
            cboLoaiNhanVien.ValueMember = "IDLoaiNhanVien";
            cboLoaiNhanVien.DisplayMember = "TenLoaiNhanVien";
            cboLoaiNhanVien.DataBindings.Add(Const.BIND_SELECTED_VALUE, _bindingSource, "IDLoaiNhanVien", true);

            cboTinhThanh.DataSource = Cache.Context.DM_TinhThanh;
            cboTinhThanh.ValueMember = "IDTinhThanh";
            cboTinhThanh.DisplayMember = "TenTinhThanh";
            cboTinhThanh.DataBindings.Add(Const.BIND_SELECTED_VALUE, _bindingSource, "IDTinhThanh", true);

            colDanToc.DataSource = Cache.Context.DM_DanToc;
            colDanToc.ValueMember = "IDDanToc";
            colDanToc.DisplayMember = "TenDanToc";

            colTonGiao.DataSource = Cache.Context.DM_TonGiao;
            colTonGiao.ValueMember = "IDTonGiao";
            colTonGiao.DisplayMember = "TenTonGiao";

            colPhongBan.DataSource = Cache.Context.DM_PhongBan;
            colPhongBan.ValueMember = "IDPhongBan";
            colPhongBan.DisplayMember = "TenPhongBan";

            //colChucVu.DataSource = Cache.Context.DM_ChucVu;
            //colChucVu.ValueMember = "IDChucVu";
            //colChucVu.DisplayMember = "TenChucVu";

            colChucVu.DataSource = Cache.Context.ACL_NhomNguoiDung;
            colChucVu.ValueMember = "IDNhomNguoiDung";
            colChucVu.DisplayMember = "TenNhom";            

            colLoaiNhanVien.DataSource = Cache.Context.DM_LoaiNhanVien;
            colLoaiNhanVien.ValueMember = "IDLoaiNhanVien";
            colLoaiNhanVien.DisplayMember = "TenLoaiNhanVien";

            colTinhThanh.DataSource = Cache.Context.DM_TinhThanh;
            colTinhThanh.ValueMember = "IDTinhThanh";
            colTinhThanh.DisplayMember = "TenTinhThanh";

            colNguoiTao.DataSource = Cache.Context.DT_NhanVien;
            colNguoiTao.ValueMember = "IDNhanVien";
            colNguoiTao.DisplayMember = "TenDayDu";

            colTrangThaiNhanVien.DataSource = Cache.Context.DM_TrangThaiNhanVien;
            colTrangThaiNhanVien.ValueMember = "IDTrangThaiNhanVien";
            colTrangThaiNhanVien.DisplayMember = "TenTrangThaiNhanVien";

            base.AddDataBinding();
        }

        protected override void RefreshData()
        {
            base.RefreshData();
            UICommon.ShowDataNotFound(dgvNhanVien, lblTimKiem, false);
        }

        protected override void AddNew()
        {
            base.AddNew();
            chbxGioiTinh.Checked = false;
            chbxDaKetHon.Checked = false;
            txtHoVaTenDem.Focus();
        }
        
        protected override int UpdateData()
        {
            if (base.UpdateData() > 0)
            {
                IQueryable<DT_NhanVien> lstNhanVien = this._bindingSource.DataSource as IQueryable<DT_NhanVien>;
                _business.UpdateNhanVien(lstNhanVien);
                return base.UpdateData();
            }
            LoadData();

            return -1;

        }

        protected override void Search()
        {
            string tuKhoa = tscbxTimKiem.Text;

            var data = from e in Cache.Context.DT_NhanVien
                       
                       where e.HoVaTenDem.Contains(tuKhoa)||
                       e.TenNhanVien.Contains(tuKhoa)||
                       e.SoCMND.Contains(tuKhoa)||
                       e.DiaChi.Contains(tuKhoa)||
                       e.Email.Contains(tuKhoa)||
                       e.TenNhanVien.Contains(tuKhoa)||
                       e.SoDienThoai.Contains(tuKhoa)||
                       e.Fax.Contains(tuKhoa)||
                       e.GhiChu.Contains(tuKhoa)
                       
                       select e;

            List<DT_NhanVien> lstEmployee = data.ToList<DT_NhanVien>();

            //Hien thi thong bao khi khong tim thay du lieu
            UICommon.ShowDataNotFound(dgvNhanVien, lblTimKiem,lstEmployee.Count == 0);

            _bindingSource.DataSource = data;
            dgvNhanVien.DataSource = _bindingSource;
        }

        protected override void Delete()
        {
            DT_NhanVien sp = _bindingSource.Current as DT_NhanVien;
            if (sp == null)
            {
                UICommon.ShowMessegeInfo(Const.MSG_KHONG_XOA_VI_SLUONG_LON_HON_KHONG);
                tsbXoa.Enabled = false;
                return;
            }
            else
            {
                sp.IDTrangThaiNhanVien = (int?)StructsEnums.EnumTrangThaiNhanVien.DaNghiLam;
                tsbXoa.Enabled = true;
            }            
        }

        #endregion

        #region Private methods

        private void InitializeForm()
        {
            _business = new DT_NhanVienBLL();

            UICommon.AddTrackingColumn(this.dgvNhanVien);
            this.dgvNhanVien.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dgvNhanVien_RowPostPaint);
            //this.txtMaNhanVien.ReadOnly = true;

            //add controls
            this._btnCapNhatTrangThai = new ToolStripButton();
            this._btnCapNhatTrangThai.AutoSize = true;
            this._btnCapNhatTrangThai.ForeColor = System.Drawing.Color.Black;
            //this._btnCapNhatTrangThai.Image = global::STLGROUP.Layers.Properties.Resources.process;
            this._btnCapNhatTrangThai.ImageTransparentColor = System.Drawing.Color.Magenta;
            this._btnCapNhatTrangThai.Name = "tsbCapNhatTrangThai";
            this._btnCapNhatTrangThai.Padding = new System.Windows.Forms.Padding(3, 0, 4, 0);
            //this._btnCapNhatTrangThai.Size = new System.Drawing.Size(150,27);
            this._btnCapNhatTrangThai.Text = "Cập nhật trạng thái";
            this._btnCapNhatTrangThai.Visible = true;
            _btnCapNhatTrangThai.Click += new EventHandler(_btnCapNhatTrangThai_Click);

            toolStrip1.Items.Insert(8, _btnCapNhatTrangThai);
        }

        #endregion

        #region Events

        private void dgvNhanVien_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void dgvNhanVien_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dg = (DataGridView)sender;
            // Current row record
            string rowNumber = (e.RowIndex + 1).ToString();

            // Format row based on number of records displayed by using leading zeros
            while (rowNumber.Length < dg.RowCount.ToString().Length)
            {
                rowNumber = "0" + rowNumber;
            }

            // Position text
            SizeF size = e.Graphics.MeasureString(rowNumber, this.Font);
            if (dg.RowHeadersWidth < (int)(size.Width + 20))
            {
                dg.RowHeadersWidth = (int)(size.Width + 20);
            }

            // Use default system text brush
            Brush b = SystemBrushes.ControlText;

            // Draw row number
            e.Graphics.DrawString(rowNumber, dg.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
        }

        private void picHinhAnh_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Title = "Vui lòng chọn file hình ảnh";
            //open.InitialDirectory = "('*.jpg'||('*.jpg'))";
            open.Multiselect = false;

            DialogResult result = open.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                Image img = UICommon.AddImage(open.FileName, Const.MAX_IMAGE);
                if (img != null)
                {
                    picHinhAnh.Image = img;
                    _current.HinhAnh = UICommon.ImageToByteArray(img);
                } 
            }
            else
            {
                picHinhAnh.Image = null;
            }
        }

        private void _bindingSource_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                _current = (DT_NhanVien)_bindingSource.Current;
                if (_current.HinhAnh != null)
                {
                    UICommon.ShowImage(UICommon.ByteArrayToImage( _current.HinhAnh), picHinhAnh);
                }
                else
                {
                    UICommon.ShowImage(QuanLyBanHang.Properties.Resources.click_chon_anh, picHinhAnh);
                }
            }
            catch
            {
                _current = null;
            }
        }

        #endregion

        private void btnDanToc_Click(object sender, EventArgs e)
        {
            UICommon.ShowPopup(new CF004(), StructsEnums.EnumDanhMuc.DanToc, cboDanToc, colDanToc);
            if (CF004.GetIDDanToc() != 0)
            {
                cboDanToc.SelectedValue = CF004.GetIDDanToc();
            }
        }

        private void btnPhongBan_Click(object sender, EventArgs e)
        {
            UICommon.ShowPopup(new CF006(), StructsEnums.EnumDanhMuc.PhongBan, cboPhongBan, colPhongBan);
            if (CF006.GetIDPhongBan() != 0)
            {
                cboPhongBan.SelectedValue = CF006.GetIDPhongBan();
            }
        }

        private void btnTinhThanh_Click(object sender, EventArgs e)
        {
            UICommon.ShowPopup(new CF002(), StructsEnums.EnumDanhMuc.TinhThanh, cboTinhThanh, colTinhThanh);
        }

        private void btnTonGiao_Click(object sender, EventArgs e)
        {
            UICommon.ShowPopup(new CF005(), StructsEnums.EnumDanhMuc.TonGiao, cboTonGiao, colTonGiao);
            if (CF005.GetIDTonGiao() != 0)
            {
                cboTonGiao.SelectedValue = CF005.GetIDTonGiao();
            }
        }

        private void btnChucVu_Click(object sender, EventArgs e)
        {
            UICommon.ShowPopup(new CF001(), StructsEnums.EnumDanhMuc.ChucVu, cboChucVu, colChucVu);
        }

        private void btnLoaiNhanVien_Click(object sender, EventArgs e)
        {
            UICommon.ShowPopup(new CF007(), StructsEnums.EnumDanhMuc.LoaiNhanVien, cboLoaiNhanVien, colLoaiNhanVien);
            if (CF007.GetIDLoaiNhanVien() != 0)
            {
                cboLoaiNhanVien.SelectedValue = CF007.GetIDLoaiNhanVien();
            }
        }

        private void _btnCapNhatTrangThai_Click(object sender, EventArgs e)
        {
            if (_current != null)
            {
                PF003 frm = new PF003() { NhanVien = _current, Context = Cache.Context };
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Cache.Context.SaveChanges();
                }
            }
        }

    }
}
